<?php
namespace Biz\service\theParagraph;

use Biz\cache\theparagraph\Banner;
use Biz\model\theParagraph\ShopBanner as mShopBanner;
use Biz\cache\theparagraph\Banner as cBanner;
use Doraemon\tools\ImgUrl;
/**
 * @deprecated
 */
class ShopBanner
{
	public static function bannerListForApp($location = mShopBanner::LOCATION_HOME_PAGE) {
		$bannerList = self::bannerList($location);
		$rst = [];
		foreach ($bannerList as $k => $banner) {
			if ($location == mShopBanner::LOCATION_HOME_PAGE) {
				foreach ($banner as $item) {
					$rst[$k][] = [
						'id' => $item['id'],
						'title' => $item['title'],
						'link_type' => $item['link_type'],
						'link_des' => $item['link_des'],
						'banner_url' => $item['banner_url'],
					];
				}
			}
		}

		return $rst;
	}

	public static function bannerList($location = mShopBanner::LOCATION_HOME_PAGE, $from = 'app') //banner location 代表客户端一个页面
	{
		mShopBanner::checkConstance(mShopBanner::LOCATION_PREFIX, $location);
		$showTypes = array_keys(mShopBanner::$showTypeDesc[$location]);

		//基于一个页面某个位置缓存
		list($cateData, $showTypes) = cBanner::getCache($location, $showTypes);
		if ($showTypes) {
			$brannerList = mShopBanner::getCurrentBannerList($location, $showTypes);
			foreach ($brannerList as $location => $DatasGrpShowType) {
				foreach ($showTypes as $showType) {
					if (isset($DatasGrpShowType[$showType])) {
						$cateData = $cateData + [$showType =>$DatasGrpShowType[$showType]];
						Banner::setCache($location, $showType, $DatasGrpShowType[$showType], self::_calExpire($location, $showType, $DatasGrpShowType[$showType]) );
					} else {
						Banner::setCache($location, $showType, []);
					}
				}
			}
		}
		foreach ($cateData as $showType => $items) {
			foreach ($items as $item) {
				if ($from == 'app') {
					$cateData[ self ::_appField($showType) ][] = self::_formatBannerInfo( $item);
					unset($cateData[$showType]);
				} else {
					$cateData[$showType][] = self::_formatBannerInfo( $item);
				}
			}
		}
		return $cateData;
	}

	private static function _appField($showType)
	{
		$appFieldMaps = [
			mShopBanner::SHOW_TYPE_HOME_PAGE_TOP => 'home_page_top',
			mShopBanner::SHOW_TYPE_HOME_PAGE_CATE_LIST => 'home_page_cate_list',
		];
		if (isset($appFieldMaps[$showType])) {
			return $appFieldMaps[$showType];
		}
		throw new \Exception('showtype 类型不存在');
	}

	private static function _formatBannerInfo($banner) {
		$banner['banner_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $banner['banner'], ImgUrl::IMAGE_SIZE_OP_ORI);
		return $banner;
	}

	private static function _calExpire($location, $showType,  $datas)
	{
		$expire = 86400 * 7;
		foreach ($datas as $data) {
			$newExpire = strtotime($data['stop_time']) - time();
			if ($newExpire < $expire) {
				$expire = $newExpire;
			}
		}
		$dateTime = date('Y-m-d H:i:s', time() + $expire);
		$banner = mShopBanner::getNoActionBanner($location, $showType, $dateTime);
		if ($banner) {
			$expire = strtotime($banner['start_time']) - time();
		}
		return $expire;
	}


}